<template>

  <div>
    <div class="filter-container">
      <el-input v-model="listQuery.title"
                placeholder="订单号"
                style="width: 100px;"
                class="filter-item"
                @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.importance"
                 placeholder="订单状态"
                 clearable style="width: 110px"
                 class="filter-item">
        <el-option v-for="item in importanceOptions"
                   :key="item"
                   :label="item"
                   :value="item" />
      </el-select>
      <el-select v-model="listQuery.type"
                 placeholder="Type"
                 clearable class="filter-item" style="width: 130px">
        <el-option v-for="item in calendarTypeOptions"
                   :key="item.key"
                   :label="item.display_name+'('+item.key+')'"
                   :value="item.key" />
      </el-select>
      <el-select v-model="listQuery.sort"
                 style="width: 140px"
                 class="filter-item" >
        <el-option v-for="item in sortOptions"
                   :key="item.key"
                   :label="item.label"
                   :value="item.key" />
      </el-select>
      <el-button v-waves class="filter-item"
                 type="primary"
                 icon="el-icon-search"
                 @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;"
                 type="primary"
                 icon="el-icon-edit"
                 @click="handleCreate">
        新增商品
      </el-button>
      <el-button v-waves :loading="downloadLoading"
                 class="filter-item"
                 type="primary"
                 icon="el-icon-download"
                 @click="handleDownload">
        导出
      </el-button>
      <el-button v-waves
                 :loading="downloadLoading"
                 class="filter-item"
                 type="primary"
                 icon="el-icon-upload2"
                 @click="">
        导入
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" prop="id" sortable="custom" align="center" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column label="商家单号" prop="productame"
                       sortable="custom" align="center" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.order_number }}</span>
        </template>
      </el-table-column>

      <el-table-column label="物流单号" prop="originalPrice"
                       sortable="custom" align="center" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.wuliu }}</span>
        </template>
      </el-table-column>

      <el-table-column label="下单时间" prop="picpath"
                       sortable="custom" align="center" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.orderdate }}</span>
        </template>
      </el-table-column>

      <el-table-column label="订单状态" prop="picpath"
                       sortable="custom" align="center" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.zhangtai }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="300" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            修改
          </el-button>
          <el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDel(row)">
            删除
          </el-button>
          <el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDel(row)">
            上架
          </el-button>
          <el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDel(row)">
            下架
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination small v-show="total>0"
                :total="total"
                :page.sync="listQuery.page"
                :limit.sync="listQuery.limit"
                @pagination="getList" />

    <el-dialog :title="textMap[dialogStatus]"
               :visible.sync="dialogFormVisible">
      <el-form ref="dataForm"
               :model="temp"
               label-position="left" label-width="130px" style="width: 400px; margin-left:50px;">
        <el-form-item label="商品名称" prop="productame">
          <el-input v-model="temp.productame" />
        </el-form-item>
        <el-form-item label="商品现价" prop="originalPrice">
          <el-input v-model="temp.originalPrice" />
        </el-form-item>
        <el-form-item label="商品原价" prop="price">
          <el-input v-model="temp.price" />
        </el-form-item>
        <el-form-item label="商品图片" prop="picpath">
          <el-input v-model="temp.picpath" />
        </el-form-item>
        <el-form-item label="库存" prop="inventory">
          <el-input v-model="temp.inventory" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary"
                   @click="dialogStatus==='create'?createData():updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
  import $ from 'jquery';
  import ajax from '@/utils/baseurl'
  import Pagination from '@/components/Pagination'
  import waves from '@/directive/waves'

  var p;
  export default {
    name: 'Goods',
    components: { Pagination },
    directives: { waves },
    data() {
      return {
        temp: {
          productame :'',
          originalPrice:'',
          price:'',
          picpath:'',
          inventory:'',
          buyNumber:''
        },
        dialogFormVisible:false,
        dialogStatus: '',
        downloadLoading:false,
        textMap: {
          update: '修改',
          create: '新增'
        },
        listQuery: {
          page: 1,
          limit: 10,
          importance: undefined,
          title: undefined,
          type: undefined,
          sort: '+id'
        },
        total: 0,
        tableKey:0,
        listLoading:false,
        list:null,
        importanceOptions: ['已完结订单', '正在配送订单', '自提订单'],
        calendarTypeOptions: [
          { key: 'CN', display_name: 'China' },
          { key: 'US', display_name: 'USA' },
          { key: 'JP', display_name: 'Japan' },
          { key: 'EU', display_name: 'Eurozone' }
        ],
        sortOptions: [{ label: '时间 升序', key: '+id' },
          { label: '时间 降序', key: '-id' }]
      }

    },
    created() {
      p=this;
      this.getList()
    },
    methods:{
      updateData(){
        this.createData();
        this.getList();
      },
      resetTemp() {
        this.temp = {
          productame :'',
          originalPrice:'',
          price:'',
          picpath:'',
          inventory:'',
          buyNumber:''
        }
      },
      handleUpdate(row){
        this.temp = Object.assign({}, row);
        this.dialogStatus = 'update';
        this.dialogFormVisible = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()//清除验证
        })

      },
      handleDel(row){
        var p = this;
        this.$confirm('确认删除吗? 是否继续', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          ajax("goods/goodsDel",{"id": row.id },function(data){
            if(data){
              p.getList();
              p.$message({
                type: 'success',
                message: '删除成功!'
              });
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      sortChange() {

      },
      handleFilter(){
        this.listQuery.page = 1;
        this.getList()
      },
      createData(){
        //this.resetTemp();
        var p=this;
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            ajax(p.dialogStatus=='create'?"goods/goodsAdd":"goods/goodsUpdate",this.temp,function(data){
              if(data){
                p.dialogFormVisible = false;
                p.getList();
                p.$notify({
                  message: p.dialogStatus==='create'?'添加成功':'修改成功',
                  type: 'success',
                  duration: 2000
                });
              }
            })
          }
        })
      },
      handleCreate(){
        this.dialogStatus = 'create';
        this.dialogFormVisible = true;
      },
      handleDownload(){

      },
      getList(){
        let p = this;
        this.listLoading = true;
        console.log(this.listQuery);
        ajax("findOrderGunli",{},function(data){
          data = JSON.parse(data);
          console.log(data);
          p.list=data;
          p.total=10;
          p.listLoading=false;
        })
      },
    }
  }
</script>













